package practise_stack;

import java.util.Stack;

class MinStack {
    private MinStack mS;
    private Stack<Integer> stack;
    private Stack<Integer> minStack;

    public MinStack() {
        this.stack = new Stack<>();
        this.minStack = new Stack<>();
    }

    public void push(int val) {
        if(minStack.isEmpty() || minStack.peek() >= val) {
            minStack.push(val);
        }
        stack.push(val);
    }

    public void pop() {
        if(!minStack.isEmpty() && !stack.isEmpty()) {
            if(minStack.peek().equals(stack.peek())) {
                minStack.pop();
            }
            stack.pop();
        }
    }
    
    public int top() {
        return stack.peek();
    }
    
    public int getMin() {
        if(!minStack.isEmpty()) {
            return minStack.peek();
        }
        return 0;
    }
}
